package crawler;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import data.stock.NewsStory;

import work.Job;

/**
 * The DocumentJob object contains the work for parsing
 * one document's url for its information.
 * @author Kevin Dolan
 */
public class DocumentJob implements Job {

	@Override
	public Object run(Object parameters, HashMap<String, Object> shared) {
		VirtualBrowser browser = (VirtualBrowser) shared.get("browser");
		String url = (String) parameters;
		System.out.println("Accessing:"+url);
		
		String body = browser.request(false, url, null, null);
		try {
			NodeList headerNodes = (new Parser(body)).extractAllNodesThatMatch(new CssSelectorNodeFilter(".docCitationContainer .headerBlack"));
			String title = headerNodes.elementAt(0).toPlainTextString();
			
			NodeList pubNodes = (new Parser(body)).extractAllNodesThatMatch(new CssSelectorNodeFilter(".docCitationContainer .textMedium"));
			String publication = pubNodes.elementAt(0).toPlainTextString();
			
			NodeList linkNodes = (new Parser(body)).extractAllNodesThatMatch(new CssSelectorNodeFilter(".docCitationContainer .textMedium a"));
			SimpleDateFormat fmt = new SimpleDateFormat("MMM dd, yyyy");
			Date date = null;
			for(int i = 0; i < linkNodes.size(); i++) {
				date = fmt.parse(linkNodes.elementAt(i).toPlainTextString(), new ParsePosition(0));
				if(date != null)
					break;
			}
			if(date == null) {
				System.err.println("Fail(Date):"+url);
				return null;
			}
			
			NodeList docSections = (new Parser(body)).extractAllNodesThatMatch(new CssSelectorNodeFilter(".docSection"));
			String summary, content;
			if(docSections.size() > 1) {
				summary = docSections.elementAt(0).toPlainTextString();
				content = docSections.elementAt(1).toPlainTextString();
			} else {
				String text = docSections.elementAt(0).toPlainTextString();
				if(text.indexOf("Full Text") >= 0) {
					summary = "";
					content = text;
				}
				else {
					throw new NullPointerException();
				}
			}
			
			System.out.println("Success:"+url);
			return new NewsStory(date.getTime(), publication, title, summary, content);
		} catch (ParserException e) {
			System.err.println("Fail(Parser):"+url);
			return null;
		}  catch (NullPointerException e) {
			System.err.println("Fail(Null):"+url);
			return null;
		} catch (IllegalArgumentException e) {
			System.err.println("Fail(Illegal):"+url);
			return null;
		}
	}

}
